package com.hyk.commonLib.common.utils;

import android.app.Activity;

import androidx.annotation.IntDef;

import com.hyk.commonLib.LibInitializer;
import com.hyk.commonLib.common.dialogView.ConfirmDialog;
import com.hyk.commonLib.common.entity.INetObj;
import com.hyk.commonLib.common.entity.ImageSource;
import com.hyk.commonLib.common.listener.OnClickResultListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class DialogUtils {
    @IntDef({SUCCESS_TYPE, NORMAL_TYPE, ERROR_TYPE, WARNING_TYPE, NOPICTURE_STYLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface DialogType {
    }

    public static final int SUCCESS_TYPE = 1;
    public static final int NORMAL_TYPE = 2;
    public static final int ERROR_TYPE = 3;
    public static final int WARNING_TYPE = 4;
    public static final int NOPICTURE_STYLE = 5;

    @IntDef({PROGRESS_TITLE_LOADING, PROGRESS_TITLE_SUBMIT, PROGRESS_TITLE_SENDING, PROGRESS_TITLE_LOGIN, PROGRESS_TITLE_WAITING})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ProgressDialogTitle {
    }

    public static final int PROGRESS_TITLE_LOADING = 1;
    public static final int PROGRESS_TITLE_SUBMIT = 2;
    public static final int PROGRESS_TITLE_SENDING = 3;
    public static final int PROGRESS_TITLE_LOGIN = 4;
    public static final int PROGRESS_TITLE_WAITING = 5;

    public static LoadingPopupView showProgressDialog(Activity activity, @ProgressDialogTitle int dialogTitle) {
        LoadingPopupView progressDialog = DialogUtils.getProgressDialog(activity, dialogTitle);
        if (activity != null && !activity.isFinishing() && !progressDialog.isShow()) {
            progressDialog.show();
        }
        return progressDialog;
    }

    public static LoadingPopupView showProgressDialog(Activity activity, String dialogTitle) {
        LoadingPopupView progressDialog = DialogUtils.getProgressDialog(activity, dialogTitle);
        if (activity != null && !activity.isFinishing() && !progressDialog.isShow()) {
            progressDialog.show();
        }
        return progressDialog;
    }

    public static void showProgressDialog(Activity activity, LoadingPopupView defaultProgressDialog) {
        if (activity != null && defaultProgressDialog != null && !activity.isFinishing() && !defaultProgressDialog.isShow()) {
            defaultProgressDialog.show();
        }
    }

    public static void closeProgressDialog(Activity activity, LoadingPopupView progressDialog) {
        if (activity != null && progressDialog != null && !activity.isFinishing()) {
            try {
                progressDialog.dismiss();
            } catch (Exception ignored) {
            }
        }
    }

    public static LoadingPopupView getProgressDialog(Activity activity, String str) {
        if (LibInitializer.isConfigLoadingDialogLayout()) {
            return new XPopup.Builder(activity).asLoading(str, LibInitializer.getLoadingDialogLayoutId(), LoadingPopupView.Style.Spinner);
        } else {
            return new XPopup.Builder(activity).asLoading(str);
        }
    }

    public static LoadingPopupView getProgressDialog(Activity activity, @ProgressDialogTitle int dialogTitle) {
        String str = "请稍候……";
        switch (dialogTitle) {
            case PROGRESS_TITLE_LOADING:
                str = "加载中……";
                break;
            case PROGRESS_TITLE_LOGIN:
                str = "登录中……";
                break;
            case PROGRESS_TITLE_SENDING:
                str = "发送中……";
                break;
            case PROGRESS_TITLE_SUBMIT:
                str = "提交中……";
                break;
            case PROGRESS_TITLE_WAITING:
                str = "请稍候……";
                break;
        }
        return getProgressDialog(activity, str);
    }


    public static void showOneButtonDialog(Activity context,
                                           String title,
                                           String content,
                                           String btnText,
                                           final OnClickResultListener onClickResultListener,
                                           boolean cancelable,
                                           @DialogType int dialogType) {
        if (context != null && !context.isFinishing()) {
            ImageSource icon;
            switch (dialogType) {
                case SUCCESS_TYPE:
                    icon = ImageSource.fromLottie(ImageSource.LottieRes.fromAssetFileName("success.json"));
                    break;
                case ERROR_TYPE:
                    icon = ImageSource.fromLottie(ImageSource.LottieRes.fromAssetFileName("error.json"));
                    break;
                case WARNING_TYPE:
                    icon = ImageSource.fromLottie(ImageSource.LottieRes.fromAssetFileName("warning.json"));
                    break;
                case NORMAL_TYPE:
                case NOPICTURE_STYLE:
                default:
                    icon = null;
                    break;
            }
            ConfirmDialog confirmDialog = new ConfirmDialog(context);
            confirmDialog.getDataBinding().getModel()
                    .setTopImage(icon)
                    .setTitle(title)
                    .setContent(content)
                    .setButtonType(ConfirmDialog.ButtonType.SINGLE_CONFIRM)
                    .setSingleBtnText(btnText)
                    .setOnSingleBtnClickListener(onClickResultListener);

            new XPopup.Builder(context)
                    .dismissOnBackPressed(cancelable) // 按返回键是否关闭弹窗，默认为true
                    .dismissOnTouchOutside(cancelable) // 点击外部是否关闭弹窗，默认为true
                    .asCustom(confirmDialog)
                    .show();
        }
    }

    public static void showTwoButtonDialog(Activity context,
                                           CharSequence title, String content,
                                           final CharSequence positiveBtnText, CharSequence negativeBtnText,
                                           final OnClickResultListener positiveCallback,
                                           final OnClickResultListener negativeCallback,
                                           boolean cancelable, @DialogType int dialogType) {

        if (context != null && !context.isFinishing()) {
            ImageSource icon;
            switch (dialogType) {
                case SUCCESS_TYPE:
                    icon = ImageSource.fromLottie(ImageSource.LottieRes.fromAssetFileName("success.json"));
                    break;
                case ERROR_TYPE:
                    icon = ImageSource.fromLottie(ImageSource.LottieRes.fromAssetFileName("error.json"));
                    break;
                case WARNING_TYPE:
                    icon = ImageSource.fromLottie(ImageSource.LottieRes.fromAssetFileName("warning.json"));
                    break;
                case NORMAL_TYPE:
                case NOPICTURE_STYLE:
                default:
                    icon = null;
                    break;
            }
            ConfirmDialog confirmDialog = new ConfirmDialog(context);
            confirmDialog.getDataBinding().getModel()
                    .setTopImage(icon)
                    .setTitle(title)
                    .setContent(content)
                    .setButtonType(ConfirmDialog.ButtonType.CONFIRM_AND_CANCEL_EQUALLY)
                    .setLeftBtnText(negativeBtnText)
                    .setOnLeftBtnClickListener(negativeCallback)
                    .setRightBtnText(positiveBtnText)
                    .setOnRightBtnClickListener(positiveCallback);

            new XPopup.Builder(context)
                    .dismissOnBackPressed(cancelable) // 按返回键是否关闭弹窗，默认为true
                    .dismissOnTouchOutside(cancelable) // 点击外部是否关闭弹窗，默认为true
                    .asCustom(confirmDialog)
                    .show();
        }

    }

    public static void showServerRequestFailDialog(final Activity context, INetObj<?> baseNetObj) {
        showOneButtonDialog(context, baseNetObj.getMessage(), null, "确定", null, false, ERROR_TYPE);
    }
}
